﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using DCSLibrary;
using System.Data;
using Utils.DatabaseUtils;
using System.Collections;
using Utils.DCSControl;
using Utils.Entity;
using Utils.DC.Web.UI;
using Utils;

public partial class Admin_Default : dcPage
{
    FunctionPageUI objFunctionPage = new FunctionPageUI();
    CategoryPageUI objCategoryPage = new CategoryPageUI();
    
    protected override void Init()
    {
        
    }

    protected override void Load()
    {
        if (!IsPostBack)
        {
            //if (pageSettings.PageAlias != "he-thong-quan-tri-noi-dung")
            //{
                if (CurrentUser != null)
                {
                    if (CurrentUser.UserType != (byte)Common.USER_TYPE.Admin)
                    {
                        Response.Write(new MessageBox().Alert("Tài khoản của bạn không có quyền truy cập chức năng admin", "/Default.aspx"));
                    }
                    lblWelcome.Text = "Welcome " + CurrentUser.UserName + ".";
                    BindMenuList();
                    General.WriteLog(Request,Server);
                }
                else
                {
                    //UserPanel.Visible = false;
                    UserPanel.Visible = false;
                    if (pageSettings.PageAlias != "he-thong-quan-tri-noi-dung")
                    {
                        
                        Response.Redirect("/Admin/");
                    }
                }
          
        }
    }

    protected void SignOut_Click(object sender, EventArgs e)
    {
        PageUtility.SighOut(Session);
        //TODO xoa standard
        Response.Redirect("/Admin/Default.aspx");
    }

    #region Cac ham thao tac du lieu

    private void BindMenuList()
    {
        if (CurrentUser.RoleList != null && CurrentUser.RoleList.Count > 0)
        {
            RadPanelItem item = null;

            RadPanelItem itemCap2 = null;

            //Lay ve cac trang admin
            DataSet dsAdmin = objFunctionPage.GetByType(true);

            //Lay ve tat ca cac trang category
            DataSet dsAdminCategoryPage = objCategoryPage.GetAll();

            DataTable dt = null;
            DataRow rowCap2 = null;
            IEnumerator ie = null;

            if (dsAdmin != null && dsAdmin.Tables.Count > 0)
            {
                DataTable dtVisibleAdmin = TableLib.CreateTable(dsAdmin.Tables[0], "IsVisible = 1");

                foreach (DataRow row in dtVisibleAdmin.Rows)
                {
                    item = new RadPanelItem(row["FunctionPageName"].ToString(), "#");
                    item.CssClass = "Menu";
                    AdminMenu.Items.Add(item);

                    dt = TableLib.CreateTable(dsAdminCategoryPage.Tables[0], "ParentID = " + row["FunctionPageID"].ToString() + " and IsVisible = 1");
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        ie = dt.Rows.GetEnumerator();
                        while (ie.MoveNext())
                        {
                            //TODO bo phan / trong link menu
                            rowCap2 = (DataRow)ie.Current;
                            itemCap2 = new RadPanelItem(rowCap2[1].ToString().Trim(), "/Admin/" + row["FunctionPageAlias"].ToString().Trim() + "/" + rowCap2["FunctionPageAlias"].ToString().Trim() + ".aspx");

                            if(CheckRole(Convert.ToInt32(rowCap2["FunctionPageID"])))
                            {
                                   item.Items.Add(itemCap2);
                            }
                        }
                    }
                }
            }
        }
    }

    private bool CheckRole(int pageID)
    {
        foreach (UserRoleEntity role in CurrentUser.RoleList)
        {
            if (role.PageID == pageID)
            {
                return true;
            }
        }
        return false;
    }

    #endregion

}
